package com.ctgu.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ctgu.server.dao.dto.ArticleDto;
import com.ctgu.server.dao.entity.Article;
import com.ctgu.server.dao.entity.User;
import com.ctgu.server.dao.vo.ArticleVo;
import com.ctgu.server.dao.vo.Result;
import com.ctgu.server.dao.vo.UserVo;
import com.ctgu.server.mapper.ArticleMapper;
import com.ctgu.server.service.ArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctgu.server.utils.ResultUtil;
import com.github.dozermapper.core.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ctgu
 * @since 2023-05-23
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private Mapper mapper;
    @Override
    public Result insertArticle(ArticleDto articleDto) {
        User user= userService.selectUser(articleDto.getUserId());
        if(user==null){
            return ResultUtil.error("参数错误，用户不存在！");
        }

        //保存文章内容
        Article article= mapper.map(articleDto,Article.class);
        this.save(article);


        return ResultUtil.success();
    }

    @Override
    public Result getArticleById(Long id) {
        if(id==null){
            return ResultUtil.error("参数错误");
        }
        Article article= this.getById(id);

        User user= userService.selectUser(article.getUserId());
        UserVo userVo=mapper.map(user,UserVo.class);
        ArticleVo articleVo=mapper.map(article,ArticleVo.class);
        articleVo.setUser(userVo);

        return ResultUtil.success(articleVo);
    }

    @Override
    public Result updateArticleById(ArticleDto articleDto) {
        User user= userService.selectUser(articleDto.getUserId());
        if(user==null){
            return ResultUtil.error("参数错误，用户不存在！");
        }



        //创建更新数据库
        Article article= mapper.map(articleDto,Article.class);
        LambdaUpdateWrapper<Article> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Article::getId,articleDto.getUserId());
        this.update(article,lambdaUpdateWrapper);

        return ResultUtil.success();
    }

    @Override
    public Result getArticleList() {
        List<ArticleVo> articleVoList=new LinkedList<>();
        List<Article> articleList= this.list();
        //处理数据
        for(Article article:articleList){
            User user= userService.selectUser(article.getUserId());
            UserVo userVo=mapper.map(user,UserVo.class);
            ArticleVo articleVo=mapper.map(article,ArticleVo.class);
            articleVo.setUser(userVo);
            articleVoList.add(articleVo);
        }
        return ResultUtil.success(articleVoList);
    }


}
